<template>
  <div class="suggestions">
    <div class="suggestion-card">
      <div class="names-container">
        <div v-for="(name, index) in allNames" 
             :key="index" 
             class="name-item"
             :style="getRandomStyle(index)"
             @click="handleCopy(name.value)">
          <div class="name-style">{{ name.style }}</div>
          <code class="name-value" :class="getStyleClass(name.style)">{{ name.value }}</code>
        </div>
      </div>
    </div>
    
    <!-- 添加复制成功提示 -->
    <transition name="toast">
      <div v-if="showToast" class="copy-toast">
        <svg class="check-icon" viewBox="0 0 24 24" width="16" height="16">
          <path fill="currentColor" d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z"/>
        </svg>
        已复制到剪贴板
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'SuggestionsList',
  props: {
    suggestions: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      showToast: false,
      toastTimer: null
    }
  },
  computed: {
    allNames() {
      const suggestion = this.suggestions[0];
      // 处理驼峰格式的输入
      const words = suggestion.baseName
        .trim()
        // 在大写字母前添加空格，但不处理连续的大写字母
        .replace(/([a-z])([A-Z])/g, '$1 $2')
        // 处理连续的大写字母，如 "URL"
        .replace(/([A-Z])([A-Z][a-z])/g, '$1 $2')
        .toLowerCase()
        .split(/\s+/)
        .filter(word => word.length > 0);
      
      // 固定顺序的命名风格
      return [
        {
          style: '小驼峰',
          value: words.map((word, i) => 
            i === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
          ).join('')
        },
        {
          style: '大驼峰',
          value: words.map(word => 
            word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
          ).join('')
        },
        {
          style: '大写下划线',
          value: words.map(word => word.toUpperCase()).join('_')
        },
        {
          style: '小写下划线',
          value: words.map(word => word.toLowerCase()).join('_')
        },
        {
          style: '连字符',
          value: words.map(word => word.toLowerCase()).join('-')
        }
      ];
    }
  },
  methods: {
    getRandomStyle(index) {
      const randomScale = 0.95 + Math.random() * 0.1;
      const randomRotate = -1 + Math.random() * 2;
      const delay = index * 0.1;
      
      return {
        transform: `scale(${randomScale}) rotate(${randomRotate}deg)`,
        animationDelay: `${delay}s`
      };
    },
    handleCopy(text) {
      navigator.clipboard.writeText(text).then(() => {
        this.showCopyToast();
        this.$emit('copied', text);
      });
    },
    showCopyToast() {
      if (this.toastTimer) {
        clearTimeout(this.toastTimer);
      }
      this.showToast = true;
      this.toastTimer = setTimeout(() => {
        this.showToast = false;
      }, 2000);
    },
    getStyleClass(style) {
      const styleMap = {
        '小驼峰': 'camel-case',
        '大驼峰': 'pascal-case',
        '下划线': 'snake-case',
        '大写下划线': 'upper-snake-case',
        '小写下划线': 'lower-snake-case',
        '连字符': 'kebab-case'
      }
      return styleMap[style] || ''
    }
  },
  beforeDestroy() {
    if (this.toastTimer) {
      clearTimeout(this.toastTimer);
    }
  }
}
</script>

<style scoped>
.suggestions {
  margin: 2rem 0;
}

.suggestion-card {
  background: white;
  border-radius: 20px;
  padding: 2rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
}

.names-container {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  justify-content: center;
  padding: 2rem;
  background: linear-gradient(135deg, #f8faff 0%, #f0f4ff 100%);
  border-radius: 16px;
  border: 1px solid rgba(148, 163, 184, 0.1);
  box-shadow: inset 0 2px 4px rgba(148, 163, 184, 0.05);
}

.name-item {
  position: relative;
  padding: 1rem 2rem;
  background: white;
  border-radius: 12px;
  cursor: pointer;
  border: 1px solid #e2e8f0;
  animation: popIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  box-shadow: 0 2px 10px rgba(99, 102, 241, 0.1);
  animation: breathe 3s infinite ease-in-out;
}

.name-item:hover {
  background: white;
  border-color: #6366f1;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(99, 102, 241, 0.2);
}

.name-item:active {
  transform: translateY(1px);
}

.name-style {
  font-size: 0.85rem;
  color: #6366f1;
  font-weight: 500;
  background: rgba(99, 102, 241, 0.1);
  padding: 0.35rem 1rem;
  border-radius: 12px;
  letter-spacing: 0.5px;
}

.name-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 1.1rem;
  white-space: nowrap;
  letter-spacing: 1px;
  padding: 0.25rem 0;
  transition: color 0.3s ease;
}

.copy-toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: white;
  padding: 0.75rem 1.25rem;
  border-radius: 9999px;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
}

.check-icon {
  color: #4ade80;
}

/* Toast 动画 */
.toast-enter-active,
.toast-leave-active {
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.toast-enter,
.toast-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(20px);
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@media (max-width: 640px) {
  .suggestion-card {
    padding: 1rem;
  }
  
  .names-container {
    gap: 1.5rem;
    padding: 1.75rem;
  }
  
  .name-item {
    padding: 1rem 1.75rem;
    gap: 0.75rem;
  }
  
  .name-value {
    font-size: 1.1rem;
    letter-spacing: 1.2px;
  }
  
  .copy-toast {
    bottom: 1.5rem;
    padding: 0.625rem 1rem;
    font-size: 0.8125rem;
  }
}

/* 不同命名风格的样式 */
.camel-case {
  color: #2563eb; /* 蓝色 */
}

.pascal-case {
  color: #7c3aed; /* 紫色 */
  font-weight: 500;
}

.snake-case {
  color: #059669; /* 绿色 */
  letter-spacing: 1px;
}

.upper-snake-case {
  color: #dc2626; /* 红色 */
  font-weight: 600;
  letter-spacing: 1px;
}

.lower-snake-case {
  color: #0891b2; /* 青色 */
  letter-spacing: 1px;
}

.kebab-case {
  color: #d97706; /* 橙色 */
  letter-spacing: 1px;
}

/* 悬停效果 */
.name-item:hover .name-value {
  opacity: 1;
  transform: none;
  color: #4f46e5;
}

/* 动画效果 */
.name-value {
  transition: color 0.3s ease;
}

/* 添加呼吸灯动画 */
@keyframes breathe {
  0% {
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.1);
    border-color: #e2e8f0;
  }
  50% {
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.2);
    border-color: #6366f1;
  }
  100% {
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.1);
    border-color: #e2e8f0;
  }
}
</style> 